module.exports = function (grunt) {
  'use strict';

  // Force use of Unix newlines
  grunt.util.linefeed = '\n';

  var project = grunt.file.readJSON('src/project.json');

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    clean: {
      dist: 'dist/*'
    },

    copy: {
      dist: {
        expand: true,
        cwd: 'src/',
        src: ['css/*', 'js/*', '*.html', 'images/*', 'bower_components/*'],
        dest: 'dist/html'
      }
    },

    jshint: {
      options: {
        jshintrc: 'src/js/.jshintrc'
      },
      dist: {
        src: 'src/js/main.js'
      }
    },

    jscs: {
      options: {
        config: '.jscsrc'
      },
      dist: {
        src: '<%= jshint.dist.src %>'
      }
    },

    less: {
      dist: {
        options: {
          strictMath: true,
          sourceMap: true,
          outputSourceFiles: true,
          sourceMapURL: 'style.css.map',
          sourceMapFilename: 'src/css/style.css.map'
        },
        src: 'src/less/style.less',
        dest: 'src/css/style.css'
      }
    },

    autoprefixer: {
      dist: {
        options: {
          map: true
        },
        src: 'src/css/style.css'
      }
    },

    csslint: {
      options: {
        csslintrc: '.csslintrc'
      },
      dist: [
        'src/css/style.css'
      ]
    },

    cssmin: {
      options: {
        compatibility: 'ie8',
        keepSpecialComments: '*',
        advanced: false
      },
      minifyDist: {
        src: 'src/css/style.css',
        dest: 'src/css/style.min.css'
      }
    },

    csscomb: {
      options: {
        config: '.csscomb.json'
      },
      dist: {
        src: 'src/css/style.css',
        dest: 'src/css/style.css'
      }
    },

    connect: {
      server: {
        options: {
          port: 3000,
          base: 'src/'
        }
      }
    },

    validation: {
      options: {
        path: 'src/validation-status.json',
        reportpath: 'src/validation-report.json',
        charset: 'utf-8',
        doctype: 'HTML5',
        failHard: true,
        reset: true,
        relaxerror: [
          'Element img is missing required attribute src.',
          'Attribute autocomplete not allowed on element input at this point.',
          'Attribute autocomplete not allowed on element button at this point.'
        ]
      },
      files: {
        filter: 'isFile',
        src: 'src/*.html'
      }
    },

    watch: {
      less: {
        files: 'src/less/*.less',
        tasks: 'less'
      }
    },

     twigServer: {
        server: {
          options: {
              port: 9000,
              // Change this to '0.0.0.0' to access the server from outside
              hostname: 'localhost',
              base: [
                  'src/.tmp',
                  'src/'
              ]
          }
        }
    },

    compress: {
      main: {
        options: {
          archive: 'dist/html.zip',
          mode: 'zip',
          level: 9,
          pretty: true
        },
        files: [
          {
            expand: true,
            cwd: 'dist/html',
            src: ['**'],
            dest: 'dist'
          }
        ]
      }
    }
  });

  // These plugins provide necessary tasks.
  require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  require('time-grunt')(grunt);

  grunt.registerTask('validation-html', ['validation']);

  grunt.registerTask('test', ['dist-css', 'csslint:dist', 'validation-html']);
  grunt.registerTask('test-js', ['jshint:dist', 'jscs:dist']);

  grunt.registerTask('less-compile', ['less:dist']);

  var cssTasks = ['autoprefixer:dist', 'csscomb:dist', 'cssmin:minifyDist'];
  if (project.less) {
    cssTasks.unshift('less-compile');
  }

  grunt.registerTask('dist-css', cssTasks);

  grunt.registerTask('default', ['clean:dist', 'test']);

  grunt.registerTask('build', ['test', 'copy:dist', 'compress']);
  grunt.registerTask('server', ['twigServer:server:keepalive']);
}
